<template>
    <el-row >
        <template v-for="(btn,idx) of toolBtns" >
        <el-button size="medium" :key="btn.No" v-if="btn.No==='SendHuiQian'" @click="SendFlow(btn.Oper)">{{btn.Name}}</el-button>
         <template v-else-if="btn.No==='Send'" >

           <el-button size="medium" :key="btn.No"   @click="SendFlow(btn.Oper)">{{btn.Name}}</el-button>                                  
           <el-select v-model="selectNode" id="DDL_ToNode" placeholder=""  v-show="protoNodes.length!=0" :key="idx" @change="changeToNode()">
                <el-option
                    v-for="item in protoNodes"
                    :key="item.No"
                    :label="item.Name"
                    :value="item.No">
                </el-option>
            </el-select>
 |      </template>
        <el-button size="medium" :key="btn.No" id="Btn_Send" v-else-if="btn.No==='Send'" @click="SendFlow(btn.Oper)">{{btn.Name}}</el-button>
        <el-button size="medium" :key="btn.No" v-else-if="btn.No==='Save'" @click="SaveFlow(btn.Oper)">{{btn.Name}}</el-button>
        <el-button size="medium" :key="btn.No" v-else-if="btn.No==='workcheckBtn'" @click="DialogOpen('workcheck','处理审核')">{{btn.Name}}</el-button>
        <el-button size="medium" :key="btn.No" v-else-if="btn.No==='Thread'" @click="DialogOpen('Thread','查看子线程')">{{btn.Name}}</el-button>
        <el-button size="medium" :key="btn.No" v-else-if="btn.No==='ParentForm'" @click="DialogOpen('ParentForm','父流程信息')">{{btn.Name}}</el-button>
        <el-button size="medium" :key="btn.No" v-else-if="btn.No==='TransferCustom'" @click="DialogOpen('TransferCustom','流转自定义')">{{btn.Name}}</el-button>
        <el-button size="medium" :key="btn.No" v-else-if="btn.No==='Help'" @click="HelpAlert(btn.Role)">{{btn.Name}}</el-button>
        <el-button size="medium" :key="btn.No" v-else-if="btn.No==='JumpWay'" @click="DialogOpen('JumpWay','跳转')">{{btn.Name}}</el-button>
        <el-button size="medium" :key="btn.No" v-else-if="btn.No==='Return'" @click="WorkReturn()">{{btn.Name}}</el-button>
        <el-button size="medium" :key="btn.No" v-else-if="btn.No==='Hung'" @click="DialogOpen('Hung','流程挂起')">{{btn.Name}}</el-button>
        <el-button size="medium" :key="btn.No" v-else-if="btn.No==='Shift'" @click="DialogOpen('Shift','流程移交')">{{btn.Name}}</el-button>
        <el-button size="medium" :key="btn.No" v-else-if="btn.No==='CC'" @click="DialogOpen('CC','抄送')">{{btn.Name}}</el-button>
        <el-button size="medium" :key="btn.No" v-else-if="btn.No==='Delete'" @click="DeleteFlow()">{{btn.Name}}</el-button>
        <el-button size="medium" :key="btn.No" v-else-if="btn.No==='EndFlow'" @click="StopFlow(btn.Name)">{{btn.Name}}</el-button>
        <el-button size="medium" :key="btn.No" v-else-if="btn.No==='PrintDoc'" @click="DialogOpen()">{{btn.Name}}</el-button>
        <el-button size="medium" :key="btn.No" v-else-if="btn.No==='Track'" @click="DialogOpen('Track','')">{{btn.Name}}</el-button>
        <el-button size="medium" :key="btn.No" v-else-if="btn.No==='Search'" @click="DialogOpen('Search','')">{{btn.Name}}</el-button>
        <el-button size="medium" :key="btn.No" v-else-if="btn.No==='Batch'" @click="DialogOpen('Batch','批处理')">{{btn.Name}}</el-button>
        <el-button size="medium" :key="btn.No" v-else-if="btn.No==='Askfor'" @click="DialogOpen('Askfor','加签处理')">{{btn.Name}}</el-button>
        <el-button size="medium" :key="btn.No" v-else-if="btn.No==='HuiQian'" @click="DialogOpen('HuiQian','选择会签人')">{{btn.Name}}</el-button>
        <el-button size="medium" :key="btn.No" v-else-if="btn.No==='AddLeader'" @click="DialogOpen('HuiQian','加主持人','AddLeader')">{{btn.Name}}</el-button>
        <el-button size="medium" :key="btn.No" v-else-if="btn.No==='WebOffice'" @click="DialogOpen()">{{btn.Name}}</el-button>
        <el-button size="medium" :key="btn.No" v-else-if="btn.No==='ReSet'" @click="ReSet()">{{btn.Name}}</el-button>
        <el-button size="medium" :key="btn.No" v-else-if="btn.No==='CH'" @click="DialogOpen('CH','考核规则')">{{btn.Name}}</el-button>
        <el-button size="medium" :key="btn.No" v-else-if="btn.No==='Note'" @click="DialogOpen('Note','备注')">{{btn.Name}}</el-button>
        <el-button size="medium" :key="btn.No" v-else-if="btn.No==='PR'" @click="DialogOpen('PR','优先级')">{{btn.Name}}</el-button>
        <el-button size="medium" :key="btn.No" v-else-if="btn.No==='Focus'" @click="Focus(btn.Name,idx)">{{btn.Name}}</el-button>
        <el-button size="medium" :key="btn.No" v-else-if="btn.No==='Allot'" @click="DialogOpen('Allot','')">{{btn.Name}}</el-button>
        <el-button size="medium" :key="btn.No" v-else-if="btn.No==='Confirm'" @click="ConfirmBtn(btn.Name,idx)">{{btn.Name}}</el-button>
        <el-button size="medium" :key="btn.No" v-else-if="btn.No==='PackUp_zip'" @click="DialogOpen('PackUp','打印')">{{btn.Name}}</el-button>
        <el-button size="medium" :key="btn.No" v-else-if="btn.No==='PackUp_html'" @click="DialogOpen('PackUp','打印')">{{btn.Name}}</el-button>
        <el-button size="medium" :key="btn.No" v-else-if="btn.No==='PackUp_pdf'" @click="DialogOpen('PackUp','打印')">{{btn.Name}}</el-button>
        <el-button size="medium" :key="btn.No" v-else-if="btn.No==='DBTemplate'" @click="DialogOpen()">{{btn.Name}}</el-button>
        <el-button size="medium" :key="btn.No" v-else-if="btn.No==='Btn_Office'" @click="OpenOffice()">{{btn.Name}}</el-button>
        </template>
       
        <el-dialog :title="title" :visible.sync="dialogFormVisible" :before-close="handleClose">
            <selectiveRecipient v-if="judgeOperation == 'sendAccepter'"></selectiveRecipient>
            <trackBox v-if="judgeOperation == 'Track'"></trackBox>
            <returnwork v-if="judgeOperation == 'returnwork'" :data="returnData"></returnwork>
            <ch v-if="judgeOperation == 'CH'"></ch>
            <hui-qian v-if="judgeOperation == 'huiqian'"></hui-qian>
        </el-dialog>
    </el-row>
    
</template>
<script>
import {HttpHandler} from '@/api/Gener.js';
import * as tools from '@/api/MyFlowGener.js';
import {beforeSave,beforeSend,beforeReturn,beforeDelete} from '@/datauser/jslidata/MyPublic.js';
import Vue from 'vue';
import HuiQian from '../workopt/hui-qian.vue';
import ch from '../workopt/ch.vue';
import selectiveRecipient from './selectiveRecipient.vue' // 选择接受人
import trackBox from './track.vue' // 轨迹 
import returnwork from '../workopt/returnwork.vue' //退回

//import demo from '@/demo/demo.vue';
Vue.prototype.$tools = tools
export default {
    props: ['protoNodes'],
    data(){
        return{
            toolBtns:[],
            componentList:[],
            currentView:'ch',
            dialogFormVisible:false,
            judgeOperation:'',
            selectNode:'',
            params:[],
            returnData:{},//返回select数据
            title:''
        }
    },
    methods:{
        changeToNode:function(){
            let params = this.$store.getters.getData;
            console.log('params',params)
            params.ToNode = this.selectNode;
            this.$store.commit('setData', params)
        },
        SendFlow:function(param){
          if(param.indexOf("SysCheckFrm()")!=-1){
              if(this.$tools.SysCheckFrm() == false) 
                return false;
              //保存从表信息
              this.$tools.SaveDtlAll();

              //发送前事件
              if (typeof beforeSend != 'undefined' && beforeSend instanceof Function)
                if (beforeSend() == false)
                    return false;
              //发送
              this.$tools.Send(this);
          }
          //嵌入式表单的发送
          if(param.indexOf("SendSelfFrom()")!=-1){
              if(this.$tools.SendSelfFrom() == false)
              return false;
          }
        
        },
        SaveFlow:function(param){
            //嵌入式表单的保存
           if(param.indexOf("SaveSelfFrom()")!=-1)
                this.$tools.SaveSelfFrom();
            else{
                if(this.$tools.SysCheckFrm()==false) 
                    return false;
                //保存从表信息
                this.$tools.SaveDtlAll();

                 //保存前事件
                if (typeof beforeSave != 'undefined' && beforeSave instanceof Function)
                    if (beforeSave() == false)
                        return false;

                //保存表单信息
                this.$tools.Save(this);
            }
        },
        //弹出新页面窗体
        DialogOpen:function(path,title){
            /*if(this.componentList.includes(path) === false){
                this.componentList.push(path);
                 this.$options.components[path] = require('../workopt/' + path + '.vue')
            }*/

            // 如果是轨迹图 先请求接口
            this.title=title;
            console.log("ddd");
             this.dialogFormVisible = true;
            this.judgeOperation = path;
           
        },
        //帮助提示
        HelpAlert:function(helpRole){
            this.$tools.HelpOper(helpRole,this.params.FK_Node,this);
        },


        //退回
        WorkReturn:function(){
            //增加退回前的事件
            console.log("退回");
            this.judgeWorkReturn();
            
        },

        // 退回前先要判断的逻辑
        judgeWorkReturn(){
            console.log("判断返回",this.params);
            var handler = new this.HttpHandler("BP.WF.HttpHandler.WF_WorkOpt");
            // handler.AddUrlData(); //FK_Flow, FK_Node,WorkID,Info info=退回信息字段.
            handler.AddJson(this.params);
            //初始化数据.
            var data = handler.DoMethodReturnString("Return_Init");
            console.log('退回初始化',data);
            if (data.indexOf('info@') == 0) {
                alert("退回按钮初始化接口返回数据还有错误，需要弹出信息");
                //这种情况下，有可能是自动退回，自动退回就提示退回消息.

                //设置弹出框的大小
                // $(".modal-content", parent.document).css("width", "");
                // $(".modal-content", parent.document).css("height", "300px");

                // $("#iframeReturnWorkForm", parent.document).css("height", "300px");

                // data = data.replace('info@', '');
                // data = data.replace(/null/g, '');
                // data = "<font>" + data + "</font>";
                // $("#Msg").html("");
                // $("#DIV_Nodes").html(data);
                // $("#DIV_Doc").html("");
                // $("#btnReturn").css("display", "none");
                // $("#btnIsBackTracking").css("display", "none");
                // $("#btnClose").attr("onclick", "Close(1)");
                return;
            }
            if (data.indexOf('err@') == 0) {
                alert("退回按钮初始化接口返回数据还有错误，需要弹出信息22");
                //console.error("退回窗口错误信息" + data);
                // data = data.replace('err@', '');
                // data = "<font color=red>" + data + "</font>";
                // $("#Msg").html(data);
                // //  alert(data);
                return;
            }
            this.returnData = data;
            this.performReturn();

        },

        // 执行退回
        performReturn(){
            if (typeof beforeReturn != 'undefined' && beforeReturn instanceof Function)
                if (beforeReturn() == false)
                    return false;

            if (this.$tools.Save(this) == false)
                return;
            this.DialogOpen("returnwork");
        },



        //删除流程
        DeleteFlow:function(){
            if (typeof beforeDelete != 'undefined' && beforeDelete instanceof Function)
                if (beforeDelete() == false)
                    return false;
            this.$confirm('您确定要删除改流程数据吗, 是否继续?', '', {
                confirmButtonText: '确定',
                cancelButtonText: '取消'
            }).then(() => {
                var handler = new HttpHandler("BP.WF.HttpHandler.WF_MyFlow");
                handler.AddPara("FK_Flow", this.$route.params.FK_Flow);
                handler.AddPara("FK_Node", this.$route.params.FK_Node);
                handler.AddPara("WorkID", this.$route.params.WorkID);
                var str = handler.DoMethodReturnString("DeleteFlow");
                if(str.indexOf('err@')!=-1){
                    this.$message({
                        type: 'error',
                        message: '删除失败，请查看控制台，或者联系管理员'
                    });
                    console.log(str);
                }else{
                    this.$message({
                        type: 'success',
                        message: str
                    });
                }
                
               
            });
            
           
        },
        //结束流程
        StopFlow:function(msg){
            
            this.$confirm('您确定要执行 [' + msg + '] ?', '', {
                confirmButtonText: '确定',
                cancelButtonText: '取消'
            }).then(() => {
                 var handler = new HttpHandler("BP.WF.HttpHandler.WF_MyFlow");
                handler.AddPara("FK_Flow", this.$route.params.FK_Flow);
                handler.AddPara("WorkID", this.$route.params.WorkID);
                var data = handler.DoMethodReturnString("MyFlow_StopFlow");
                if(data.indexOf('err@')!=-1){
                    this.$message({
                        type: 'error',
                        message: '结束流程失败，请查看控制台，或者联系管理员'
                    });
                    console.log(data);
                }else{
                    this.$message({
                        type: 'success',
                        message: data
                    });
                }
                
               
            });
            

        },
        //数据重置
        ReSet:function(){

        },
        //关注
        Focus:function(btnLab,idx){
            if(btnLab == "关注")
                btnLab="取消关注";
            else
                btnLab ="关注";
            this.toolBtns[idx].Name = btnLab;
            var handler = new HttpHandler("BP.WF.HttpHandler.WF_MyFlow");
            handler.AddPara("WorkID", this.$route.params.WorkID);
            handler.DoMethodReturnString("Focus"); //执行保存方法.
        },
        //确认
        ConfirmBtn:function(btnLab,idx){
             if(btnLab == "确认")
                btnLab="取消确认";
            btnLab = "确认";
             this.toolBtns[idx].Name = btnLab;
            var handler = new HttpHandler("BP.WF.HttpHandler.WF_MyFlow");
            handler.AddPara("WorkID", this.$route.params.WorkID);
            handler.DoMethodReturnString("Confirm");
        },
        OpenOffice:function(){

        },

        vuexSetData(ToNode){
            let params = this.$store.getters.getData;
            console.log('params',params)
            params.ToNode = ToNode;
            this.$store.commit('setData', params)
            console.log('vuex',this.$store.getters.getData)
        },

        //关闭
        handleClose(){
            this.dialogFormVisible = false;
            this.judgeOperation = '';
            // this.$router.push({name:"start"})
        }
       
    },
    components:{
        ch,
        HuiQian,
        selectiveRecipient,
        trackBox,
        returnwork
        //demo
    },
    created(){
        //初始化按钮
        this.params = this.$store.getters.getData;
        this.$tools.initToolBar(this);
        if(this.protoNodes.length!=0)
            this.selectNode = this.protoNodes[0].No;
        console.log("this.selectNode",this.selectNode)
        this.vuexSetData(this.selectNode)
    },
    mounted(){
        this.$Bus.$off("closeMsg");
        this.$Bus.$on("closeMsg", item=>{
            this.dialogFormVisible = false;
            if(item == '退回'){
                this.$router.push({name:'todolist'})
            }
        });
    }

}
</script>

<style lang="less" scoped>
/deep/ .el-dialog__body{
	padding: 10px 20px;
}
</style>